import os
import sys

def rename_to_uppercase(path):
    if os.path.isdir(path):
        # 如果是文件夹，先处理文件夹内的所有文件和子文件夹
        for root, dirs, files in os.walk(path, topdown=False):
            for name in files + dirs:
                old_name_path = os.path.join(root, name)
                new_name_path = os.path.join(root, name.upper())
                if old_name_path != new_name_path:
                    os.rename(old_name_path, new_name_path)
                    print(f"重命名: {old_name_path} -> {new_name_path}")
        
        # 最后处理拖入的文件夹本身
        parent_dir = os.path.dirname(path)
        folder_name = os.path.basename(path)
        new_folder_name = folder_name.upper()
        new_folder_path = os.path.join(parent_dir, new_folder_name)
        if path != new_folder_path:
            os.rename(path, new_folder_path)
            print(f"重命名文件夹: {path} -> {new_folder_path}")
    elif os.path.isfile(path):
        # 如果是文件，直接重命名
        new_path = path.upper()
        if path != new_path:
            os.rename(path, new_path)
            print(f"重命名: {path} -> {new_path}")
    else:
        print(f"错误：{path} 既不是有效的文件也不是有效的文件夹。")

def main():
    if len(sys.argv) != 2:
        print("用法: python script.py <path>")
        return
    path = sys.argv[1]
    print(f"处理路径: {path}")
    rename_to_uppercase(path)
    print("处理完成。")

if __name__ == "__main__":
    main()
